package com.jsj.isdt.servlet;

import com.alibaba.fastjson.JSON;
import com.jsj.isdt.entity.Supplier;
import com.jsj.isdt.utils.DBUtils;
import com.jsj.isdt.utils.ResultData;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

public class ServletSupplier extends HttpServlet {
    @Override
    protected void  doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String action = req.getParameter("action") == null ? "" : req.getParameter("action");
        switch (action){
            case  "getdglist":
                getDglist(req,resp);
                break;
            case  "add":
                    Add(req,resp);
                break;
            case  "exists":
                Exists(req,resp);
                break;
            case  "delete":
                Delete(req,resp);
                break;
            case  "getone":
                getOne(req,resp);
                break;
            case  "update":
                Update(req,resp);
                break;
            default:
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req,resp);
    }
    private  void  getDglist (HttpServletRequest req, HttpServletResponse resp)  {
        try {
            int  pageIndex = req.getParameter("page") ==null? 1: Integer.parseInt(req.getParameter("page"));
            int  pagesize = req.getParameter("row") ==null? 10: Integer.parseInt(req.getParameter("row"));
            String suppliername = req.getParameter("suppliername");
            PrintWriter out = resp.getWriter();
            HashMap<String, Object> map = new HashMap<>();
            String strwhere="";
            if (suppliername == null || suppliername.equals(""))
                strwhere= "";
            else
                strwhere =" where suppliername like '%"+suppliername+"%'";
            String sqlcount;
                sqlcount ="select count(*) from supplier" +strwhere;
            int total = Integer.parseInt(DBUtils.QueryScalar(sqlcount).toString());
            map.put("total", total);
            String sqllist = "select * from supplier "+strwhere+" limit "+(pageIndex-1)*pagesize+','+pagesize;
            List<Supplier> list = DBUtils.testDemoQueryBeanList(sqllist, Supplier.class);
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    private  void  Add (HttpServletRequest req, HttpServletResponse resp) throws IOException  {
        try {
            PrintWriter out = resp.getWriter();
            String supplierno = req.getParameter("supplierno");
            String suppliername =  new String(req.getParameter("suppliername").getBytes("iso-8859-1"),"utf-8");
            String address      =  new String(req.getParameter("address").getBytes("iso-8859-1"),"utf-8");
            String contacts =  new String(req.getParameter("contacts").getBytes("iso-8859-1"),"utf-8");
            String telephone =  new String(req.getParameter("telephone").getBytes("iso-8859-1"),"utf-8");

            String sql = "insert into supplier(supplierno,suppliername,address,contacts,telephone) values(?,?,?,?,?)";
            if (DBUtils.testDemoUpdate(sql, supplierno, suppliername, address, contacts, telephone) > 0) {
                out.write(JSON.toJSONString(new ResultData(String.valueOf(resp.getStatus()), "保存成功")));
            } else {
                out.write(JSON.toJSONString(new ResultData("501", "保存失败")));
            }
        }
        catch (Exception e)
        {
            resp.getWriter().write(JSON.toJSONString(new ResultData("501", "数据有误无法保存")));
        }
    }
    private void  Exists(HttpServletRequest req, HttpServletResponse resp){
        try {
            PrintWriter out = resp.getWriter();
            String supplierno = req.getParameter("supplierno");
            String sql = "select count(*) from supplier where supplierno = ?";
            int count = Integer.parseInt(DBUtils.QueryScalar(sql,supplierno).toString());

            if (count ==1){
                out.write("false");
            }
            else {
                out.write("true");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private  void  Delete(HttpServletRequest req, HttpServletResponse resp){
        try {
            PrintWriter out = resp.getWriter();
            String supplierno = req.getParameter("supplierno");
            String sql = "delete from supplier where supplierno =?";
            if (DBUtils.testDemoUpdate(sql,supplierno) >0){
                out.write(JSON.toJSONString(new ResultData("200","删除成功")));
            }
            else
                out.write(JSON.toJSONString(new ResultData("500","删除失败")));

        }catch (Exception e){
            e.printStackTrace();
        }
    }
private  void  getOne(HttpServletRequest req, HttpServletResponse resp){

    resp.setContentType("text/html;charset=utf-8");
    resp.setCharacterEncoding("utf-8");
        try {
            PrintWriter out = resp.getWriter();
            String supplierno = req.getParameter("supplierno");
            String sql = "select * from supplier where supplierno= ?";
      Supplier supplier =   DBUtils.QueryBean(sql,Supplier.class,supplierno);
      ResultData rd= new ResultData();
      if (supplier != null){
          rd.setCode(String.valueOf(resp.getStatus()));
          rd.setMsg("获取成功");
          rd.setData(supplier);
          out.write(JSON.toJSONString(rd));
      }
      else {
          rd.setCode("S01");
          rd.setMsg("获取失败");
          out.write(JSON.toJSONString(rd));
      }

        }catch (Exception e){
            e.printStackTrace();
        }
}
    private  void  Update (HttpServletRequest req, HttpServletResponse resp) throws IOException  {

//        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        try {
            PrintWriter out = resp.getWriter();
            String supplierno = req.getParameter("supplierno");
            String suppliername =  new String(req.getParameter("suppliername").getBytes("iso-8859-1"),"utf-8");
            String address      =  new String(req.getParameter("address").getBytes("iso-8859-1"),"utf-8");
            String contacts =  new String(req.getParameter("contacts").getBytes("iso-8859-1"),"utf-8");
            String telephone = req.getParameter("telephone");
            String sql = "update  ignore supplier set  suppliername = ?,address = ?,contacts = ?,telephone = ?,supplierno = ? ";
            if (DBUtils.testDemoUpdate(sql,suppliername, address, contacts, telephone,supplierno) > 0) {
                out.write(JSON.toJSONString(new ResultData(String.valueOf(resp.getStatus()), "保存成功")));
            } else {
                out.write(JSON.toJSONString(new ResultData("501", "保存失败")));
            }
        }
        catch (Exception e)
        {
            resp.getWriter().write(JSON.toJSONString(new ResultData("501", "数据有误无法保存")));
        }
    }

    }



